package com.caesar.Prime;

import java.util.Scanner;

/**
 * @author caesar
 * @date 2025/7/19
 * @Description:找出1000以内的所有是2的幂次方减1的素数
 * @apiNote
 */
public class FindPrime_2 {
    public static boolean isPrime(int num) {//素数
        if (num <= 1) return false;
        if (num == 2) return true;
        for (int i = 3; i <= Math.sqrt(num); i += 1) {
            if (num % i == 0) return false;
        }
        return true;
    }

    public static void findMersennePrimes(int size) {//是2的幂次方减1的素数
        int n = 1;
        while (true) {
            int mersenne = (1 << n) - 1; // 计算 2^n - 1
            if (mersenne >= size) break;
            if (isPrime(mersenne)) {
                System.out.println("符合条件的数: " + mersenne + " (2^" + n + "-1)");
            }
            n++;
        }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入想要找数据的范围：");
        int size = input.nextInt();
        findMersennePrimes(size);
        input.close();

    }
}
